<!--
title: 文件管理
description: 
published: true
date: 2025-06-17T04:30:31.410Z
tags: 
editor: ckeditor
dateCreated: 2025-06-17T04:30:31.410Z
-->

<h1>上传文件</h1>
<p>&nbsp;</p>
<pre><code class="language-plaintext"># 上传文件
file_id = sdk_upload_file(filename, data, length, content_type, extension)</code></pre>
<p>&nbsp;</p>
<ul>
  <li>参数说明：<br>filename：文件名。值为 string<br>data：BytesIO对象。值为 Object<br>length：BytesIO的bytes的长度。值为 int<br>content_type：文件类型。值为 string<br>extension：文件扩展名。值为 string</li>
  <li>返回说明<br>file_id：文件ID，用于查询文件和下载文件。</li>
</ul>
<p>&nbsp;</p>
<h2><strong>简单示例</strong></h2>
<p>&nbsp;</p>
<pre><code class="language-plaintext"># 以接口代码为例

# 导入依赖库
import uuid
from io import BytesIO

# 导入SDK
global sdk_upload_file

# 结构文件对象开头
extension = "txt" # 扩展名
data = b"Hello Word!"
bio = BytesIO()
bio.write(data)
bio.seek(0)  # 重置指针到文件
filename = "Hello Word" + "." + str(extension) # 文件名

# 上传文件
file_id = sdk_upload_file(filename, bio, len(data), "text/plain", extension)
if file_id:
	return file_id
return "上传失败！"</code></pre>
<p>&nbsp;</p>
<h3>补充说明</h3>
<ul>
  <li>平台前端提供通用上传文件接口：<br>URL：/api/upload/&lt;project_id&gt; [POST]<br>DATA:&nbsp;<br>参数说明：<br>&nbsp; &nbsp; &nbsp; &nbsp;project_id：项目ID<br><br>连接示例：</li>
</ul>
<pre><code class="language-plaintext">vue 伪代码示例
uploadUrl = http://local.tools.com/api/upload/XXXXXXX

&lt;el-upload :action="uploadUrl" :show-file-list="false"&gt;
  &lt;el-button size="mini" type="primary"&gt;上传&lt;/el-button&gt;
&lt;/el-upload&gt;</code></pre>
<p>&nbsp;</p>
<h1>下载文件</h1>
<p>&nbsp;</p>
<pre><code class="language-plaintext"># 下载文件
file = sdk_download_file(file_id)</code></pre>
<p>&nbsp;</p>
<ul>
  <li>参数说明：<br>file_id：文件ID。值为 string</li>
  <li>返回说明<br>file：BaseHTTPResponse对象，用flask的send_file下载。</li>
</ul>
<p>&nbsp;</p>
<h2><strong>简单示例</strong></h2>
<p>&nbsp;</p>
<pre><code class="language-plaintext"># 以接口代码为例

# 导入依赖库
from flask import send_file

# 导入SDK
global sdk_download_file, log_print

filename = "hello word.txt"
file_id = "a9539488-49cd-11f0-a139-5a7757e581fd.txt"
file = sdk_download_file(file_id)
if file:
    # 读文件内容
    content = file.read().decode("utf-8")
    # 打印文件内容
    log_print(content, "INFO")
    # 下载文件
    return send_file(file, as_attachment=True, download_name=filename)

return "文件不存在"</code></pre>
<p>&nbsp;</p>
<h2>补充说明</h2>
<ul>
  <li>平台前端提供直接下载文件接口：<br>URL：/api/download/&lt;project_id&gt;/&lt;file_id&gt;?token=&lt;token&gt; [GET]<br><br>参数说明：<br>&nbsp; &nbsp; &nbsp; &nbsp;project_id：项目ID<br>&nbsp; &nbsp; &nbsp; &nbsp;file_id：文件ID<br>&nbsp; &nbsp; &nbsp; &nbsp;token：登陆认证TOKEN<br><br>连接示例：</li>
</ul>
<pre><code class="language-plaintext">http://local.tools.com/api/download/XXXXXX/XXXXX.txt?token=XXXX</code></pre>
<p>&nbsp;</p>
<h1>获文件信息</h1>
<p>&nbsp;</p>
<pre><code class="language-plaintext"># 获文件信息
file_info = sdk_get_file(id)</code></pre>
<p>&nbsp;</p>
<ul>
  <li>参数说明：<br>id：平台文件ID。值为 string</li>
  <li>返回说明<br>file_info：字典对象。值为&nbsp;<br>&nbsp;</li>
</ul>
<pre><code class="language-plaintext">{
   "createTime": "2025-06-15 17:03:41",
   "createUser": "fa11e670-2413-11f0-aa51-0a4444b8d20c",
   "dataLevel": "normal",
   "description": null,
   "id": "a1d20f1a-49c7-11f0-8acf-66613f5b6ee6", # 这个是平台文件ID
   "name": "6a9ddf72-6522-11ef-8e65-0ad3783d570c.json",
   "path": "/api/download/b90ba90e-2431-11f0-a3b3-f2cf1bfaab0e/a1cf310a-49c7-11f0-8acf-66613f5b6ee6.json",
   "pid": "None",
   "projectId": "b90ba90e-2431-11f0-a3b3-f2cf1bfaab0e",
   "size": 0,
   "updateTime": "2025-06-15 17:03:41",
   "updateUser": "fa11e670-2413-11f0-aa51-0a4444b8d20c",
   "url": "a1cf310a-49c7-11f0-8acf-66613f5b6ee6.json" # 这个值就是文件上传与文件下载的file_id
}</code></pre>
<p>&nbsp;</p>
<h2><strong>简单示例</strong></h2>
<p>&nbsp;</p>
<pre><code class="language-plaintext"># 以接口代码为例

# 导入SDK
global sdk_get_file

# 平台文件ID
id = "a1d20f1a-49c7-11f0-8acf-66613f5b6ee6"
file_info = sdk_get_file(id)

return file_info</code></pre>
<p>&nbsp;</p>
<h1>获项目文件列表</h1>
<p>&nbsp;</p>
<pre><code class="language-plaintext"># 获项目文件列表
files = sdk_get_all_files()</code></pre>
<p>&nbsp;</p>
<ul>
  <li>返回说明<br>files：字典对象。值为&nbsp;<br>&nbsp;</li>
</ul>
<pre><code class="language-plaintext">[
  {
   "createTime": "2025-06-15 17:03:41",
   "createUser": "fa11e670-2413-11f0-aa51-0a4444b8d20c",
   "dataLevel": "normal",
   "description": null,
   "id": "a1d20f1a-49c7-11f0-8acf-66613f5b6ee6", # 这个是平台文件ID
   "name": "6a9ddf72-6522-11ef-8e65-0ad3783d570c.json",
   "path": "/api/download/b90ba90e-2431-11f0-a3b3-f2cf1bfaab0e/a1cf310a-49c7-11f0-8acf-66613f5b6ee6.json",
   "pid": "None",
   "projectId": "b90ba90e-2431-11f0-a3b3-f2cf1bfaab0e",
   "size": 0,
   "updateTime": "2025-06-15 17:03:41",
   "updateUser": "fa11e670-2413-11f0-aa51-0a4444b8d20c",
   "url": "a1cf310a-49c7-11f0-8acf-66613f5b6ee6.json" # 这个值就是文件上传与文件下载的file_id
  },
  ...
]</code></pre>
<p>&nbsp;</p>
<h2><strong>简单示例</strong></h2>
<p>&nbsp;</p>
<pre><code class="language-plaintext"># 以接口代码为例

# 导入SDK
global sdk_get_all_files

# 获取项目文件列表
files = sdk_get_all_files()

return files</code></pre>
<p>&nbsp;</p>
